/******************************************************************************
 * @File name   :      ShortMessageTemplateController.java
 *
 * @Author      :      chen shixiang
 *
 * @Date        :      2011-7-8
 *
 * @Copyright Notice: 
 * Copyright (c) 2011 SGM, Inc. All  Rights Reserved.
 * This software is published under the terms of the SGM Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2011-7-8 下午06:34:45        chen shixiang     1.0            Initial Version
 * 2011-9-15 下午02:50:45        HeHao     1.1            Initial Version
 * 2011-10-17 下午02:11:33        HeHao           1.2     Change Method getShortMessageTemplateForUpdate
 * 2011-10-21 下午02:22:36        HeHao           1.3     Change Method getShortMessageTemplateListWithPaging and updateShortMessageTemplate
 * 2011-10-27 下午04:47:08        HeHao           1.4     Fix Bug
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.smstemplate.controller;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cap.bts.commonfunc.sms.domain.SmsTemplate;
import com.cap.bts.commonfunc.sms.dto.ShortMessageTemplateDetailDto;
import com.cap.bts.commonfunc.sms.dto.ShortMessageTemplateQueryDto;
import com.cap.bts.commonfunc.sms.dto.ShortMessageTemplateResultDto;
import com.cap.bts.commonfunc.sms.service.IShortMessageTemplateService;
import com.cap.bts.framework.codemapping.service.ICodeMapping;
import com.cap.bts.framework.common.constant.CommonConstants;
import com.cap.bts.framework.common.controller.BaseController;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.usermgnt.constant.OrganizationType;
import com.cap.bts.framework.usermgnt.domain.User;
import com.cap.bts.framework.usermgnt.service.IOrganizationMgntService;

/**
 *
 */
@Controller
@RequestMapping(value="/sms")
public class ShortMessageTemplateController extends BaseController {

    private static final String SMS_TEMP_SESSION="smsTemplateSession";
    
    @Autowired
    IShortMessageTemplateService shortMessageTemplateService;
    @Autowired
    ICodeMapping codeMapping ;
    @Autowired
    IOrganizationMgntService orgService;
    /**
     * 默认到列表页面的类
     * @return ModelAndView
     */
    @RequestMapping(value="/defaultlist")
    public ModelAndView getShortMessageTemplateWithDetault(HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("initPage", true);
        ShortMessageTemplateQueryDto queryDto = new ShortMessageTemplateQueryDto();
        map.put("queryDto",queryDto);
        return getShortMessageTemplateListWithPaging(queryDto, request);
    }
    
    /**
     * 根据输入的查询DTO查询列表信息
     * @param queryDto ShortMessageTemplateQueryDto
     * @return ModelAndView
     */
    @RequestMapping(value="/backtoquerysmstemplates",method=RequestMethod.POST)
    public ModelAndView backToGetShortMessageTemplateListWithPaging(ShortMessageTemplateQueryDto queryDto,HttpServletRequest request){
        User user = getCurrentUser();
        ShortMessageTemplateQueryDto tempQueryDto=queryDto;
        tempQueryDto.setBrandId(Integer.valueOf(user.getBrandId()+""));
        tempQueryDto = (ShortMessageTemplateQueryDto)request.getSession().getAttribute(SMS_TEMP_SESSION);
        return getShortMessageTemplateListWithPaging(tempQueryDto, request);
    }
    
    /**
     * 根据输入的查询DTO查询列表信息
     * @param queryDto ShortMessageTemplateQueryDto
     * @return ModelAndView
     */
    @RequestMapping(value="/querysmstemplates",method=RequestMethod.POST)
    public ModelAndView getShortMessageTemplateListWithPaging(ShortMessageTemplateQueryDto queryDto,HttpServletRequest request){
        User user = getCurrentUser();
        queryDto.setBrandId(Integer.valueOf(user.getBrandId()+""));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("queryDto", queryDto);
        List<ShortMessageTemplateResultDto> resultDtoList = shortMessageTemplateService.getShortMessageTemplate(queryDto);
        map.put("resultDtoList", resultDtoList);
        map.put(CommonConstants.REQUEST_ATTRIBUTE_PAGER, queryDto.getPager());
        request.getSession().setAttribute(SMS_TEMP_SESSION, queryDto);
        return new ModelAndView("sms/query_sms_templates",map);
    }
    /**
     * 修改短信模板内容
     * @Date        :      2011-7-8
     * @param detailDto ShortMessageTemplateDetailDto
     * @return ModelAndView
     * @throws AppException AppException
     * @throws ParseException 
     */
    @RequestMapping(value="/updatesmstemplate",method=RequestMethod.POST)
    public ModelAndView updateShortMessageTemplate(ShortMessageTemplateDetailDto detailDto,HttpServletRequest request) throws AppException{
        ShortMessageTemplateQueryDto queryDto;
        try {
            User user = getCurrentUser();
            detailDto.setBrandId(Long.valueOf(user.getBrandId()+""));
            shortMessageTemplateService.updateShortMessageTemplate(detailDto);
            queryDto = (ShortMessageTemplateQueryDto) request.getSession().getAttribute(SMS_TEMP_SESSION);
        } catch (Exception e) {
            throw new AppException("updatesmstemplate exception",e);
        }
        return getShortMessageTemplateListWithPaging(queryDto, request);
    }
    /**
     * 根据页面选择的id把模板生效时间和过期时间置于当前时间
     * @Date        :      2011-7-8
     * @param id Long
     * @return ModelAndView
     * @throws AppException  AppException
     */
    @RequestMapping(value="/delete/{smsTemplateId}",method=RequestMethod.GET)
    public ModelAndView deleteShortMessageTemplateById(@PathVariable("smsTemplateId")Long id,HttpServletRequest request) throws AppException{
        shortMessageTemplateService.deleteShortmessageTemplateById(id);
        ShortMessageTemplateQueryDto queryDto = (ShortMessageTemplateQueryDto) request.getSession().getAttribute(SMS_TEMP_SESSION);
        return getShortMessageTemplateListWithPaging(queryDto, request);
    }
    /**
     * 根据模板id查询模板和相关关联信息到修改页面
     * @Date        :      2011-7-8
     * @param id Long
     * @return ModelAndView
     * @throws AppException AppException
     */
    @RequestMapping(value="/edit/{smsTemplateId}",method=RequestMethod.GET)
    public ModelAndView getShortMessageTemplateForUpdate(@PathVariable("smsTemplateId")Long smsTemplateId) throws AppException{
        Map<String, Object> map = new HashMap<String, Object>();
        User user = getCurrentUser();
        ShortMessageTemplateDetailDto variableDeataiDto = new ShortMessageTemplateDetailDto();
        variableDeataiDto.setId(smsTemplateId);
        toDetailValue(smsTemplateId, map, variableDeataiDto);
        map.put("rfsList", orgService.getOrgByOrgType(OrganizationType.RFS, user.getBrandId())) ;
        return new ModelAndView("sms/edit_sms_template",map);
    }

    /**
     * @Date        :      2011-7-11
     * @param id
     * @param map
     * @param variableDeataiDto
     * @throws AppException
     */
    private void toDetailValue(Long id, Map<String, Object> map,
        ShortMessageTemplateDetailDto variableDeataiDto) throws AppException {
        ShortMessageTemplateDetailDto detailDto = shortMessageTemplateService.getShortMessageTemplateById(id);
        variableDeataiDto.setTemplateCode(detailDto.getTemplateCode());
        map.put("detailDto", detailDto);
        List<SmsTemplate> codeTemplates = shortMessageTemplateService.findTemplateByTemplateCode(detailDto.getTemplateCode(), detailDto.getEffectScope());
        map.put("codeTemplates", codeTemplates);
        variableDeataiDto.setBrandId(detailDto.getBrandId());
        map.put("variableList", shortMessageTemplateService.findAllVariableWithPaging(variableDeataiDto));
        map.put("variableDto", variableDeataiDto);
        map.put(CommonConstants.REQUEST_ATTRIBUTE_PAGER, variableDeataiDto.getPager());
    }
    /**
     * 点击详细页面的变量下一页调用
     * @Date        :      2011-7-11
     * @param deataiDto ShortMessageTemplateDetailDto
     * @return ModelAndView
     * @throws AppException AppException
     */
    @RequestMapping(value="/getVariableByPaging",method=RequestMethod.POST)
    public ModelAndView getVariableByPaging(ShortMessageTemplateDetailDto deataiDto) throws AppException{
        Map<String, Object> map = new HashMap<String, Object>();
        toDetailValue(deataiDto.getId(), map, deataiDto);
        return new ModelAndView("sms/edit_sms_template",map);
    }
    /**
     * 设置总部跟进计划模板和设置指定经销商跟进计划模板导出SGM&Dealer
     * @Date :2011-10-19 
     * @param QueryFollowupPlanTemplateDto queryFollowupPlanTemplateDto
     * @return ModelAndView
     * @throws AppException 
     * 
     */
     @RequestMapping(value = "/exportShortMessageTemplate")
     public ModelAndView exportShortMessageTemplate(ShortMessageTemplateQueryDto queryDto) throws AppException {
         User user = getCurrentUser();
         queryDto.setBrandId(Integer.valueOf(user.getBrandId()+""));
         queryDto.setIsExport(true);
         ModelAndView modelAndView = new ModelAndView();
         List<ShortMessageTemplateResultDto> resultDtoList = shortMessageTemplateService.getShortMessageTemplate(queryDto);
         modelAndView.addObject(CommonConstants.EXPORT_CSV_DATA_LIST, resultDtoList);
         modelAndView.addObject(CommonConstants.EXPORT_CSV_MAPPING_FILE, "exportShortMessageTemplate.xml");
         modelAndView.addObject(CommonConstants.EXPORT_CSV_TEMPLATE_NAME, "exportShortMessageTemplateMappings"); 
         modelAndView.setViewName("exportShortMessageTemplate");
         return modelAndView;
     }
}
